package com.sgb.kjwl.view.ui.adapter.self.logistics;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.CarriageBusinessEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.transport.OrderBargainingActivity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 说明：承运商机  适配器 （物流运输  货主）
* 名称：md
* */

    public class BusinessAdapter  extends BaseRecycleAdapter <CarriageBusinessEntity>   {

    String areaCode;

    public BusinessAdapter(Context context,  List<CarriageBusinessEntity> datas,String areaCode) {
        super(context, R.layout.item_carriage_business, datas);
        this.areaCode=areaCode;
    }



    @Override
    public void convert(ItemViewHolder holder, CarriageBusinessEntity carriageBusinessEntity) {


        //设置绑定数据
        holder.setBinding(BR.viewModel, carriageBusinessEntity);
        ImageView ivTag=holder.itemView.findViewById(R.id.iv_tag);
        holder.itemView.findViewById(R.id.tV_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               showDialog(carriageBusinessEntity.getContrctsNumber());
               // CarrierBargainingActivity.start(context);
            }
        });

        holder.itemView.findViewById(R.id.tv_yi_jia).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderBargainingActivity.start(context, OrderBargainingActivity.BARGAINING,carriageBusinessEntity.getCarrierNo(),carriageBusinessEntity.getDriverNo());
             //   OverbookingPublishActivity.start(context,"1");
            }
        });

        holder.itemView.findViewById(R.id.tv_ding_dan).setOnClickListener(new View.OnClickListener() {//下单
            @Override
            public void onClick(View v) {
                OrderBargainingActivity.start(context, OrderBargainingActivity.ORDER, carriageBusinessEntity.getCarrierNo(),carriageBusinessEntity.getDriverNo());
              //  OverbookingPublishActivity.start(context,"2");
            }
        });
        holder.itemView.findViewById(R.id.rl_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getUrl(carriageBusinessEntity.getDriverNo());
            }
        });
        switch (carriageBusinessEntity.getState()){
            case 1:
                holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.GONE);
                holder.itemView.findViewById(R.id.tv_yi_jia).setVisibility(View.GONE);
                ivTag.setImageResource(R.mipmap.img_tag2);
                break;
            case 2:
                holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.GONE);
                holder.itemView.findViewById(R.id.tv_yi_jia).setVisibility(View.GONE);
                ivTag.setImageResource(R.mipmap.img_tag4);
                break;
        }
        if(carriageBusinessEntity.getPattern()==0&&carriageBusinessEntity.getState()==0){//普通模式，待下单
            ivTag.setImageResource(R.mipmap.img_tag3);
            holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.VISIBLE);
            holder.itemView.findViewById(R.id.tv_yi_jia).setVisibility(View.GONE);
        }else if(carriageBusinessEntity.getPattern()==1&&carriageBusinessEntity.getState()==0){//竞价模式，竞价中
            ivTag.setImageResource(R.mipmap.img_tag1);
            holder.itemView.findViewById(R.id.tv_ding_dan).setVisibility(View.GONE);
            holder.itemView.findViewById(R.id.tv_yi_jia).setVisibility(View.VISIBLE);
        }
        holder.itemView.findViewById(R.id.ll_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getH5Url3(carriageBusinessEntity.getCarrierNo());

            }
        });

    }

/*

    //临时加的，测试数据展示的
    @Override
    public int getItemCount() {
        return 5;
    }

*/


    //点击的拨打电话的吐司弹框
    private void showDialog() {
        Log.e("BargainingAdater","1");
        View view = View.inflate(context, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("确认呼叫");
        tv_title.setVisibility(View.VISIBLE);
        tv_message.setText("132-0588-5987");
        tv_logout_confirm.setText("确定");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                    Intent intent = new Intent(Intent.ACTION_DIAL);
//                    Uri data = Uri.parse("tel:" + "4000010051");
                //                    intent.setData(data);
                //                    startActivity(intent);
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
        public void getUrl(String id){
            DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getTeamPriceAPI().getUrlSetting("ownerDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
                @Override
                public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null || response.body().getState() == null) {
                        return;
                    }

                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        String url = response.body().getData().getUrl() + "?id=" + id+"&areaCode="+areaCode;
                        WebViewX5HostActivity.start(context,url);
                        LogUtil.v("carsInfo=", url);
                    } else {
                        MToast.showToast(context, response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                }
            });
        }
        //打电话
        public void showDialog(String phone) {
            View view = View.inflate(context, R.layout.dialog_two_btn, null);
            final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
            roundCornerDialog.show();
            roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

            TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
            TextView tv_title = view.findViewById(R.id.tv_title);
            TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
            tv_title.setVisibility(View.VISIBLE);
            TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
            tv_title.setText("确认呼叫");
            tv_message.setText(phone);

            //确定
            tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + phone);
                    intent.setData(data);
                    context.startActivity(intent);

                    roundCornerDialog.dismiss();
                }
            });

            //取消
            tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    roundCornerDialog.dismiss();
                }
            });
        }
        // 承运详情
        public void getH5Url3(String id) {
            DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("carrierDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
                @Override
                public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null || response.body().getState() == null) {
                        return;
                    }
                    Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        String url = response.body().getData().getUrl() + "?id=" + id+"&areaCode="+areaCode+"&isMyMenu=true";
                        Log.e("madong_test","4====="+url);
                        WebViewX5HostActivity.start(context, url);
                    } else {
                        MToast.showToast(context, response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                }
            });
        }
}
